/*
 * @Author: 老刘
 * @Date: 2020-04-29 15:51:38
 * @Description: 生成二维码
 */
import { wxApi2Promise } from '../../../utils/util.js';
import drawQrcode from '../../../utils/qrcode.js';
Page({
	data: {
		codePath: '',
		writeOffCode: '微信搜索"动画样式库"公众号,关注我'
	},
	async onReady () {
		// 数据加载完成调用
		await this.draw();
	},
	async draw () {
		const width = 196 / 750 * wx.getSystemInfoSync().windowWidth;
		drawQrcode({
			width: width ? width : 196,
			height: width ? width : 196,
			canvasId: 'code-img',
			text: this.data.writeOffCode,
			_this: this
		});
		setTimeout(async () => {
			await this.getCodePath();
		}, 1000);
	},
	async getCodePath () {
		const that = this;
		if (that.data.codePath) {
			return;
		}
		try {
			const res = await wxApi2Promise(wx.canvasToTempFilePath, {
				x: 0,
				y: 0,
				width: 260,
				height: 180,
				canvasId: 'code-img'
			}, this);
			this.setData({
				codePath: res.tempFilePath
			});
		} catch (e) {
			console.log(e);
		}
	}
});
